from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.common.by import By
import pywinauto

# 实例化浏览器驱动，自动解决浏览器依赖
driver = webdriver.Chrome(ChromeDriverManager().install())
# 浏览器最大化
driver.maximize_window()
# 打开网易见外工作台
driver.get("https://jianwai.youdao.com/")
time.sleep(2)
# 登录存在多表单，我需要先切换进入表单，再定位账号、密码输入框
# 定位表单
frame_ele = driver.find_element(By.CSS_SELECTOR, "iframe[id]")
# 切换进入表单
# driver.switch_to_frame(frame_ele)  # 虽然会有waring警告，但是运行正常
driver.switch_to.frame(frame_ele)

# 定位邮箱输入框元素
email_input_ele = driver.find_element(By.CSS_SELECTOR, "input[name=email]")
# 输入邮箱
email_input_ele.send_keys("974381044@qq.com")
# 定位密码输入框
pwd_input_ele = driver.find_element(By.CSS_SELECTOR, "input[name=password]")
# 输入密码
pwd_input_ele.send_keys("zytc@12310120")
# 定位登录按钮
login_button_ele = driver.find_element(By.ID, "dologin")
# 点击-登录按钮
login_button_ele.click()
time.sleep(3)

# 点击-新建项目
driver.find_element(By.XPATH, "//div[@class='btn btn-add']").click()
time.sleep(1)
# 点击-图片翻译
driver.find_element(By.XPATH, "//div[@class='title' and text()='图片翻译']").click()
time.sleep(2)

# input标签文件上传，直接使用send_keys(),这里文件上传需使用绝对路径
driver.find_element(By.CLASS_NAME, "file").send_keys(r"C:\TT\习知\15\python-study-15\第15天\1624958787.png")

# # 非input标签可是使用pywinauto
# # 点击文件上传按钮，打开win控件
# driver.find_element(By.XPATH, "//div[@class='btn btn-upload']").click()
# time.sleep(1)
# app = pywinauto.Desktop()
# win = app['打开']  # ”打开“这个控件对象
# # 输入上传图片的地址
# win['Edit'].type_keys(r"C:\TT\习知\15\python-study-15\第15天\1624958787.png")
# time.sleep(1)
# # 点击-打开按钮
# win["Button"].click()

time.sleep(5)
# driver.quit()